package com.platform.toutiao.bean;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.platform.toutiao.error.TouTiaoErrorException;

import me.chanjar.weixin.common.util.json.GsonHelper;

public class TouTiaoUserInfo {
	private String openId;
	private String nickName;
	private String gender;
	private String language;
	private String city;
	private String province;
	private String country;
	private String avatarUrl;
	private Watermark watermark;

	public static TouTiaoUserInfo fromJson(String jsonStr) throws TouTiaoErrorException {
		Gson g = new Gson();
		JsonObject jsonObject = g.fromJson(jsonStr, JsonObject.class);
		if (jsonObject.get("errcode") != null) {
			throw TouTiaoErrorException.error(jsonStr);
		}
		TouTiaoUserInfo userInfo = new TouTiaoUserInfo();
		userInfo.setOpenId(GsonHelper.getString(jsonObject, "openId"));
		userInfo.setNickName(GsonHelper.getString(jsonObject, "nickName"));
		userInfo.setGender(GsonHelper.getString(jsonObject, "gender"));
		userInfo.setLanguage(GsonHelper.getString(jsonObject, "language"));
		userInfo.setCity(GsonHelper.getString(jsonObject, "city"));
		userInfo.setProvince(GsonHelper.getString(jsonObject, "province"));
		userInfo.setCountry(GsonHelper.getString(jsonObject, "country"));
		userInfo.setAvatarUrl(GsonHelper.getString(jsonObject, "avatarUrl"));

		Watermark watermark = new Watermark();
		JsonObject watermarkJson = jsonObject.get("watermark").getAsJsonObject();
		watermark.setAppid(GsonHelper.getString(watermarkJson, "avatarUrl"));
		watermark.setTimestamp(GsonHelper.getString(watermarkJson, "avatarUrl"));
		userInfo.setWatermark(watermark);
		return userInfo;
	}
	
	public static class Watermark {
		private String timestamp;
		private String appid;

		public String getTimestamp() {
			return timestamp;
		}

		public void setTimestamp(String timestamp) {
			this.timestamp = timestamp;
		}

		public String getAppid() {
			return appid;
		}

		public void setAppid(String appid) {
			this.appid = appid;
		}
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getAvatarUrl() {
		return avatarUrl;
	}

	public void setAvatarUrl(String avatarUrl) {
		this.avatarUrl = avatarUrl;
	}

	public Watermark getWatermark() {
		return watermark;
	}

	public void setWatermark(Watermark watermark) {
		this.watermark = watermark;
	}

}
